//
//  ImageEditView.m
//  TestAPP
//
//  Created by yangyong on 14-6-4.
//  Copyright (c) 2014年 gainline. All rights reserved.
//

#import "ImageEditView.h"


@implementation ImageEditView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initResource];
    }
    return self;
}

- (void)initResource
{
    self.backgroundColor = [UIColor clearColor];
    // 选择照片时候的 边框的颜色
    _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    _imageView.userInteractionEnabled = YES;
    _imageView.contentMode=UIViewContentModeScaleAspectFill;
    _imageView.clipsToBounds=YES;
    _imageView.layer.borderWidth = 2.0f;
    _imageView.layer.borderColor = [UIColor whiteColor].CGColor;
    [self addSubview:_imageView];
    

    CGFloat btnW = 17.0f;
    _deleteButton = [[UIImageView alloc] initWithFrame:CGRectMake(_imageView.width - btnW - 3.0f,
                                                               _imageView.height - btnW - 65.0f,
                                                               btnW,
                                                               btnW)];
    
//    [_deleteButton setBackgroundColor:BPGlobaBGColor];
    [_deleteButton setImage:[UIImage imageNamed:@"deleteBtn"]];
    [_imageView addSubview:_deleteButton];
    
    UITapGestureRecognizer *deleteAlasset = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteButtonAction:)];
    [self addGestureRecognizer:deleteAlasset];
    
}

- (void)setImageAsset:(ALAsset *)asset index:(NSInteger)index
{
    _asset = asset;
    _index = index;
    CGImageRef ref = [asset thumbnail];
    UIImage *image = [[UIImage alloc] initWithCGImage:ref];
    
    //UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [_imageView setImage:image];
        // 在选择照片的 时候 就提示 照片的像素不足
        
        _pxNoMuch = [[UIImageView alloc] initWithFrame:CGRectMake(5, 8, _imageView.frame.size.width - 10, _imageView.frame.size.height - 20)];
        _pxNoMuch.image = [UIImage imageNamed:@"nomuchpx"];
        
        if ([asset defaultRepresentation].dimensions.width < 1000) {
            [_imageView addSubview:_pxNoMuch];
        }
        
    });
}

- (void)deleteButtonAction:(id)sender
{
    if(_deleteEdit && [_deleteEdit respondsToSelector:@selector(responseToDelete:)])
    {
        [_deleteEdit responseToDelete:self];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
